package org.sky.framework.util;

/**
 * 缓存对象
 * 
 * @author gaofu
 *
 * @param <T>
 */
public class CacheEntity<T> {

	/** 过期时间 */
	private long exp;

	/** 缓存内容 */
	private T entity;

	/**
	 * 缓存是否过期
	 * 
	 * @return
	 */
	public boolean isExpire() {
		if (entity == null) {
			return true;
		}
		boolean result = System.currentTimeMillis() > exp;
		return result;
	}

	/**
	 * 重置过期时间, 默认1小时
	 */
	public void resetExp() {
		exp = System.currentTimeMillis() + 1000 * 60 * 60;
	}

	/**
	 * 重置过期时间/毫秒
	 */
	public void resetExp(long exp) {
		this.exp = System.currentTimeMillis() + exp;
	}

	/**
	 * 刷新
	 * 
	 * @param entity 对象
	 */
	public void refresh(T entity) {
		this.entity = entity;
		resetExp();
	}

	/**
	 * 刷新
	 * 
	 * @param entity 对象
	 * @param exp    过期时长
	 */
	public void refresh(T entity, long exp) {
		this.entity = entity;
		resetExp(exp);
	}

	public long getExp() {
		return exp;
	}

	public void setExp(long exp) {
		this.exp = exp;
	}

	public T getEntity() {
		return entity;
	}

	public void setEntity(T entity) {
		this.entity = entity;
	}

}
